package com.harmony.pilot.completion.lsp;

import com.harmony.pilot.completion.service.CompletionService;
import com.harmony.pilot.completion.service.DiagnosticService;
import com.harmony.pilot.completion.service.NavigationService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.launch.LSPLauncher;
import org.eclipse.lsp4j.services.LanguageClient;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Language Server Launcher
 * 用于启动LSP服务器（可以通过标准输入输出或Socket连接）
 */
@Slf4j
public class LanguageServerLauncher {
    
    private final CompletionService completionService;
    private final NavigationService navigationService;
    private final DiagnosticService diagnosticService;
    
    public LanguageServerLauncher(
            CompletionService completionService,
            NavigationService navigationService,
            DiagnosticService diagnosticService) {
        this.completionService = completionService;
        this.navigationService = navigationService;
        this.diagnosticService = diagnosticService;
    }
    
    /**
     * 通过标准输入输出启动LSP服务器
     * 这是最常见的启动方式，适用于IDE集成
     */
    public void launchWithStdio() {
        log.info("Starting Language Server with stdio");
        
        OpenHarmonyLanguageServer server = new OpenHarmonyLanguageServer(
            completionService,
            navigationService,
            diagnosticService
        );
        
        Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
            server,
            System.in,
            System.out
        );
        
        LanguageClient client = launcher.getRemoteProxy();
        server.connect(client);
        
        Future<Void> listening = launcher.startListening();
        
        try {
            listening.get();
        } catch (InterruptedException | ExecutionException e) {
            log.error("Language Server interrupted", e);
            Thread.currentThread().interrupt();
        }
    }
    
    /**
     * 通过自定义输入输出流启动LSP服务器
     */
    public void launch(InputStream in, OutputStream out) {
        log.info("Starting Language Server with custom streams");
        
        OpenHarmonyLanguageServer server = new OpenHarmonyLanguageServer(
            completionService,
            navigationService,
            diagnosticService
        );
        
        Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(
            server,
            in,
            out
        );
        
        LanguageClient client = launcher.getRemoteProxy();
        server.connect(client);
        
        Future<Void> listening = launcher.startListening();
        
        try {
            listening.get();
        } catch (InterruptedException | ExecutionException e) {
            log.error("Language Server interrupted", e);
            Thread.currentThread().interrupt();
        }
    }
}

